package com.cqengine.controller;

import com.cqengine.entity.Position;
import com.cqengine.service.Impl.PositionServiceImpl;
import com.cqengine.util.DateUtil;
import com.cqengine.util.ObjectFactoryUtil;
import com.googlecode.cqengine.IndexedCollection;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CqEngineController {
    /**
     * 测试量级
     */
    private static final int count = 10_0000;

    @RequestMapping(value = "index")
    public String index (){
        updateQty();
        delete_insert();
        return "success";
    }



    public void updateQty(){
        System.out.println("updateQty......");
        PositionServiceImpl positionService = new PositionServiceImpl(true);
        IndexedCollection<Position> posQueue = positionService.getPosQueue();

        long startTime = System.currentTimeMillis();
        initData(posQueue,count);
        long t = System.currentTimeMillis() - startTime;
        System.out.printf("加载耗时：%sms \t count: %d \t avg: %fms/ops \t tps: %fops/s \n", t, count, 1.0*t/count, 1000.0*count/t);

        // -------------------------- 查询 --------------------------
        startTime = System.currentTimeMillis();
        for (int i = 1; i <= count; i++){
            //小于i价格的都查出来
            List<Position> list = positionService.query(i);

            //更新触发器
            for(Position p : list){
                positionService.update(p,true);
            }

            //更新触发器，并更改qty
            for(Position p : list){
                Position newPosition = p.clone();
                positionService.update(p,newPosition);
            }
        }
        t = System.currentTimeMillis() - startTime;
        System.out.printf("查询耗时：%sms \t count: %d \t avg: %fms/ops \t tps: %fops/s \n\n", t, count, 1.0*t/count, 1000.0*count/t);
    }


    public void delete_insert(){
        System.out.println("delete_insert......");
        PositionServiceImpl positionService = new PositionServiceImpl(true);
        IndexedCollection<Position> posQueue = positionService.getPosQueue();

        long startTime = System.currentTimeMillis();
        initData(posQueue,count);
        long t = System.currentTimeMillis() - startTime;
        System.out.printf("加载耗时：%sms \t count: %d \t avg: %fms/ops \t tps: %fops/s \n", t, count, 1.0*t/count, 1000.0*count/t);

        // -------------------------- 查询 --------------------------
        startTime = System.currentTimeMillis();
        int insertCount = count;
        for (int i = 1; i <= count; i++){
            //小于i价格的都查出来
            List<Position> list = positionService.query(i);

            //更新触发器
            for(Position p : list){
                positionService.update(p,true);
            }

            //更新触发器，并更改删除； 再增加
            for(Position p : list){
                positionService.delete(p);

                //新增
                p.setIsTriggered(false);
                //唯一主键
                p.setId((++insertCount)+"");
                positionService.insert(p);
            }
        }
        t = System.currentTimeMillis() - startTime;
        System.out.printf("查询耗时：%sms \t count: %d \t avg: %fms/ops \t tps: %fops/s \n\n", t, count, 1.0*t/count, 1000.0*count/t);
    }

    /**
     * 初始化数据
     * @param posQueue
     * @return
     */
    public void initData(IndexedCollection<Position> posQueue, int count){
        for (int i = 1;i <= count;i++){
            Position position = new Position(i+"", ObjectFactoryUtil.getRandomNumberStr(),i,i,
                    "BTCUSDT",false,1, DateUtil.getNow(),"3000",3);
            posQueue.add(position);
        }
    }
}
